package com.xgkj.diyiketang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.GiveFlowerTask;
import com.xgkj.diyiketang.util.ToastUtil;

public class TransFlowActivity extends BaseActivity {

    private EditText mEtTransFlow;
    private Button mBtTransFlow;
    private String mUserId = null;
    private TextView mTvBack ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trans_flow);
        Intent intent = getIntent();
        if (intent != null) {
            mUserId = intent.getStringExtra("USERID");
            //Log.e("mUserId:",mUserId);
        }
        initView();


    }
    private int transflowerId = -1;
    private void requestTransFlowTask () {
                String flowerNum = mEtTransFlow.getText().toString();

        //SendFlowerTask sendflower = new SendFlowerTask(Integer.parseInt(flowerNum),mUserId,"0");
        Log.e("mUserId",mUserId+":::");
        GiveFlowerTask giveflower = new GiveFlowerTask(mUserId ,Integer.parseInt(flowerNum));
        giveflower.addListener(uploadImageListener);
        transflowerId = getEatShowApp().getTaskManager().addTask(giveflower);
    }

    private TaskEventListener uploadImageListener = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (errorCode == 8000241) {
                ToastUtil.showMessage(getApplicationContext(),"学分不够无法赠送");
            }
        }
    };

    private void initView() {
        mEtTransFlow = (EditText) findViewById(R.id.et_flow_num);
        mBtTransFlow = (Button) findViewById(R.id.transflow_btn);
        mTvBack = (TextView) findViewById(R.id.back);
        mTvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TransFlowActivity.this.finish();
            }
        });
        mBtTransFlow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    Integer.parseInt(mEtTransFlow.getText().toString());
                    requestTransFlowTask();
                    TransFlowActivity.this.finish();
//                    ToastUtil.showMessage(mContext,"转赠"+mEtTransFlow.getText().toString()+"朵花给好友成功");
//                    startActivity(new Intent(mContext,MyFlowerActivity.class));
                } catch (Exception e){
                    ToastUtil.showMessage(getApplicationContext(),"请输入有效的学分数量");
                }


            }
        });
    }

}
